import {createAsyncThunk, createSlice} from "@reduxjs/toolkit"
import { getTypeList } from "../api/typeList";

export const getType = createAsyncThunk(
    "type/getTypeList",
    async ()=>{
        try {
            const res = await getTypeList();
            // 填充返回的数据到状态仓库
            return res?.data?.data
        }catch (e){
            console.log("数据类型获取失败",e)
        }
    }
)

const typeList = createSlice({
    name:"type",
    initialState:{
        typeList:[], //存储所有的类型
        issueTypeId:"all",
        bookTypeId:"all"
    },
    reducers:{
       updateIssueTypeId:(state,{payload})=>{
        state.issueTypeId = payload;
       },
       updateBookTypeId:(state,{payload})=>{
        state.bookTypeId = payload
       }
    },
    extraReducers:(builder)=>{
        // 专门处理异步的reducers
        // 这里会有三种状态reject pending fulfilled
        builder.addCase(getType.fulfilled,(state,action)=>{
            state.typeList = action.payload;
        })
    }
})
export const {updateIssueTypeId,updateBookTypeId} = typeList.actions;
export default typeList.reducer;